package handlers

import (
	"crypto/md5"
	"encoding/hex"
	"github.com/gin-gonic/gin"
	"gopkg.in/mgo.v2/bson"
	"net/http"
	"strconv"
	"strings"
	"wx-api/adx-api/errmsg"
)

type Json struct {
	Msg       string      `json:"msg"`
	Status    int64       `json:"status"`
	RequestID string      `json:"request_id"`
	Data      interface{} `json:"data"`
}

type Controller struct {
	Ctx *gin.Context
	Json
}

func NewBaseController(c *gin.Context) *Controller {
	baseController := new(Controller)
	baseController.RequestID = bson.NewObjectId().Hex()
	baseController.Ctx = c
	return baseController
}

func (this *Controller) NoAuth() *Controller {
	this.Ctx.JSON(401, gin.H{
		"msg":  "please Login base 003",
		"code": 0,
	})

	return this
}

func (this *Controller) SetErrorMsg(data interface{}, errm errmsg.ErrorMsg) *Controller {

	this.Msg = errm.GetMessage()
	this.Status = int64(errm.GetCode())

	if errm.GetCode() == 0 {
		this.Data = data
	}
	if this.Data == nil {
		this.Data = bson.M{}
	}
	this.Ctx.JSON(http.StatusOK, this.Json)

	return this
}
func (this *Controller) SetErrorMessage(messagestr string) *Controller {
	this.Msg = messagestr
	this.Status = -1
	if this.Data == nil {
		this.Data = bson.M{}
	}
	this.Ctx.JSON(http.StatusOK, this.Json)
	return this
}
func (this *Controller) SetSuccessMessage(messagestr string) *Controller {
	this.Msg = messagestr
	this.Status = 0
	if this.Data == nil {
		this.Data = bson.M{}
	}
	this.Ctx.JSON(http.StatusOK, this.Json)
	return this
}

func (this *Controller) SetJsonResponse(json string) *Controller {
	this.Ctx.Header("Content-Type", "application/json; charset=utf-8")
	this.Ctx.String(http.StatusOK, json)
	return this
}
func (this *Controller) SetSuccess() *Controller {
	return this.SetErrorMsg(nil, errmsg.SuccessMsg())
}

func (this *Controller) SetData(data interface{}) *Controller {
	this.Data = data
	return this
}

func (this *Controller) SetStatus(status int64) *Controller {
	this.Status = status
	return this
}

func (this *Controller) SetMsg(msg string) *Controller {
	this.Msg = msg
	return this
}

func (this *Controller) MakeErrJson(msg string) {
	this.SetStatus(-1)
	this.SetMsg(msg)
	this.MakeRetJson()
}

func (this *Controller) MakeSuccessJson(msg string) {
	this.SetStatus(0)
	this.SetMsg(msg)
	this.MakeRetJson()
}

func (this *Controller) MakeRetJson() {
	this.Ctx.JSON(http.StatusOK, this.Json)
}

func (this *Controller) BadRequest() {
	this.Ctx.JSON(http.StatusBadRequest, this.Json)
}

func (this *Controller) Forbidden() {
	this.Ctx.JSON(http.StatusNotAcceptable, this.Json)
	this.Ctx.Abort()
}

//
//func (this *Controller) GetUserInfo() (uc.Uc, error) {
//	var (
//		user uc.Uc
//		ok   bool
//	)
//	u, exist := this.Ctx.Get("user")
//	if exist {
//		if user, ok = u.(uc.Uc); !ok {
//			return user, errors.New("no cookie")
//		}
//	}
//
//	return user, errors.New("no cookie")
//}

func (this *Controller) GetString(name string, defaultvalue ...string) string {
	s, ok := this.Ctx.GetQuery(name)
	if !ok {
		if len(defaultvalue) > 0 {
			return defaultvalue[0]
		}
		return ""
	}
	return s
}
func (this *Controller) GetBool(name string) bool {
	s, ok := this.Ctx.GetQuery(name)
	if !ok {
		return false
	}
	return s == "true"
}

// 获取数字参数
func (this *Controller) GetInt(name string, defaultValue int64) int64 {
	s := this.Ctx.Query(name)
	rv, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		return defaultValue
	} else {
		return rv
	}
}

// 获取数字参数
func (this *Controller) GetInt32(name string, defaultValue int) int {
	s := this.Ctx.Query(name)
	rv, err := strconv.Atoi(s)
	if err != nil {
		return defaultValue
	} else {
		return rv
	}
}

// 获取数字参数
func (this *Controller) GetFloat64(name string, defaultValue float64) float64 {
	s := this.Ctx.Query(name)
	rv, err := strconv.ParseFloat(s, 64)
	if err != nil {
		return defaultValue
	} else {
		return rv
	}
}

func (this *Controller) GetPage() (pageNum, pageSize int) {
	pageNum = this.GetInt32("pageNum", -1)
	pageSize = this.GetInt32("pageSize", -1)

	return
}

func (this *Controller) CheckSecret(str string, key string) bool {
	md := getNMd5(str)
	if md == "" {
		return false
	}
	return strings.ToUpper(md) == key
}

func getNMd5(a string) string {
	hasher := md5.New()
	hasher.Write([]byte(a))
	return hex.EncodeToString(hasher.Sum(nil))
}
